package com.changhongit.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.changhongit.entity.ServiceType;
import com.changhongit.service.ServiceTypeService;
import com.changhongit.util.Common;
import com.changhongit.util.PageView;
import com.changhongit.util.SystemConfig;

/**
 * 
 * @author JacksonYang 2014-2-13
 * @Email:
 * @version 1.0v
 */
@Controller
@RequestMapping(value = "/background/serviceProvider/type")
public class ServiceTypeController {
	@Autowired
	private ServiceTypeService serviceTypeService;
	
	@Autowired
	private SystemConfig systemConfig;

	/**
	 * 跳到新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "addUI")
	public String addUI(Model model) {
		List<ServiceType> serviceTypes = serviceTypeService.findAll();
		model.addAttribute("serviceTypes", serviceTypes);
		return "/background/serviceProvider/type/add";
	}

	/**
	 * 保存新增
	 * 
	 * @param model
	 * @param resources
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(Model model, ServiceType serviceType,
			@RequestParam(value = "file", required = false) MultipartFile file) {
		String pic = uploadImgType(file);
		serviceType.setPic(pic);
		serviceTypeService.add(serviceType);
		return "redirect:query.html";
	}

	/**
	 * 分页查询
	 * 
	 * @param model
	 * @param resources
	 * @param pageNow
	 * @return
	 */
	@RequestMapping(value = "query")
	public String query(Model model, ServiceType serviceType, String pageNow) {
		PageView pageView = null;
		if (Common.isEmpty(pageNow)) {
			pageView = new PageView(1);
		} else {
			pageView = new PageView(Integer.parseInt(pageNow));
		}
		pageView = serviceTypeService.query(pageView, serviceType);
		model.addAttribute("pageView", pageView);
		return "/background/serviceProvider/type/list";
	}

	/**
	 * 根据id删除
	 * 
	 * @param model
	 * @param resourcesId
	 * @return
	 */
	@RequestMapping(value = "deleteById")
	public String deleteById(Model model, String id) {
		serviceTypeService.delete(id);
		return "redirect:query.html";
	}

	/**
	 * 查询单条记录
	 * 
	 * @param model
	 * @param resourcesId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "getById")
	public String getById(Model model, String id, int typeKey) {
		ServiceType serviceType = serviceTypeService.getById(id);
		model.addAttribute("serviceType", serviceType);
		List<ServiceType> stLists = serviceTypeService.findAll();
		model.addAttribute("serviceTypeLists", stLists);
		if (typeKey == 1) {
			return "/background/serviceProvider/type/edit";
		} else {
			return "/background/serviceProvider/type/show";
		}
	}

	/**
	 * 更新修改的信息
	 * 
	 * @param model
	 * @param resources
	 * @return
	 */
	@RequestMapping(value = "update")
	public String updateResources(Model model, ServiceType serviceType,
			@RequestParam(value = "file", required = false) MultipartFile file) {
		String pic = uploadImgType(file);
		serviceType.setPic(pic);
		serviceTypeService.modify(serviceType);
		return "redirect:query.html";
	}

	@RequestMapping(value = "deleteAll")
	public String deleteAll(String[] check, Model model) {
		for (String id : check) {
			serviceTypeService.delete(id);
		}
		return "redirect:query.html";
	}
	
	public String uploadImgType(MultipartFile file) {
		String dir = systemConfig.getDir("dir");
		String prodir = systemConfig.getDir("prodir");
		System.out.println("file------->:" + file);
		String imgurl = "";
		if (file.getOriginalFilename() != null
				&& !"".equals(file.getOriginalFilename())) {
			System.out.println("file:" + file.getOriginalFilename());
			try {
				InputStream is = file.getInputStream();
				byte[] inOutb = new byte[is.available()];
				String uuid = UUID.randomUUID().toString().replace("-", "");
				FileOutputStream fos = new FileOutputStream(new File(dir + uuid
						+ ".jpg"));
				imgurl = prodir + uuid + ".jpg";
				is.read(inOutb);
				fos.write(inOutb);
				is.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
				return "error";
			}
		}
		return imgurl;
	}
}
